package iu.gq.db;

import iu.gq.db.items.ChapterDetail;
import iu.gq.db.items.ChapterItem;
import iu.gq.template.GQConst;
import java.util.ArrayList;
import java.util.List;

import com.gq.utils.AESCipher;
import com.gq.utils.Crypto;
import android.content.Context;
import android.database.Cursor;

public class DbAdapter extends DatabaseHelper {

	public static final String PWD_CRYPTO = "longhoanggiang";
	
	private Crypto crypto = null;
	private AESCipher cipher;
	
	public DbAdapter(Context context) {
		super(context);
		createDatabase();
		cipher = new AESCipher("longhoanggiang");
		try {
			crypto = new Crypto(PWD_CRYPTO);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<ChapterDetail> getAllChapterDetails() {
		ArrayList<ChapterDetail> details = new ArrayList<ChapterDetail>();
		openDatabase();
		if (mDb != null) {
			Cursor c = mDb.query("udv_details", new String[] { "id", "chapterId", "detail" }, null, null, null, null, null);
			if (c.moveToFirst()) {
				do {
					ChapterDetail item = new ChapterDetail();
					item.setChapterId(c.getString(c.getColumnIndex("chapterId")));
					item.setId(c.getString(c.getColumnIndex("id")));
					item.setDetail(c.getString(c.getColumnIndex("detail")));
					details.add(item);
				} while(c.moveToNext());
			}
			c.close();
		}
		close();
		return details;
	}

	@Override
	public List<ChapterItem> getAllChapterItems() {
		ArrayList<ChapterItem> items = new ArrayList<ChapterItem>();
		openDatabase();
		if (mDb != null) {
			Cursor c = mDb.query("udv_chapters", new String[] { "id", "title" }, null, null, null, null, null);
			if (c.moveToFirst()) {
				do {
					ChapterItem item = new ChapterItem();
					item.setId(c.getString(c.getColumnIndex("id")));
					item.setTitle(decryptAesCipher(c.getString(c.getColumnIndex("title"))));
					items.add(item);
				} while(c.moveToNext());
			}
			c.close();
		}
		close();
		return items;
	}

	@Override
	public ChapterDetail getChapterDetailById(String Id) {
		openDatabase();
		ChapterDetail detail = null;
		if (mDb != null) {
			Cursor c = mDb.query("udv_details", new String[] { "id", "chapterId", "detail" }, " chapterId='"+Id+"'", null, null, null, null);
			if (c.moveToFirst()) {
				detail = new ChapterDetail();
				detail.setChapterId(c.getString(c.getColumnIndex("chapterId")));
				detail.setDetail(c.getString(c.getColumnIndex("detail")));
				detail.setId(c.getString(c.getColumnIndex("id")));
			}
			c.close();
		}
		close();
		return detail;
	}

	@Override
	public ChapterItem getChapterItemById(String Id) {
		openDatabase();
		ChapterItem cItem = null;
		if (mDb != null) {
			Cursor c = mDb.query("udv_chapters", new String[] { "id", "title" }, " id='"+Id+"'", null, null, null, null);
			if (c.moveToFirst()) {
				cItem = new ChapterItem();
				cItem.setId(c.getString(c.getColumnIndex("id")));
				cItem.setTitle(decryptAesCipher(c.getString(c.getColumnIndex("title"))));
			}
			c.close();
		}
		close();
		return cItem;
	}
	
	public String decryptAesCipher(String input) {
		String result = input;
		if (GQConst.DB_ENCRYPTED) {
			try {
				result = cipher.decrypt(input);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public String decrypt(String input) {
		String result = input;
		try {
			if (GQConst.DB_ENCRYPTED)
				result = crypto.decryptFromBase64(input);
		} catch (Exception e) {
			result = input;
		}
		return result;
		
	}
}
